import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;


public class Dictionary {
	
	HashMap<String, Integer> dict = new HashMap<String, Integer>();
	
	public Dictionary(){
		
	}
	
	public void parseString(String text, Pattern pattern){
		String[] words = pattern.split(text);
		for(int i=0; i<words.length; i++){
			if(dict.containsKey(words[i])){
				Integer freq = dict.get(words[i]);
				dict.put(words[i], ++freq);
			}
			else{
				dict.put(words[i], new Integer(1));
			}
		}
	}
	
	public Integer getKeywordsCount(ArrayList<String> keywordsList){
		if(keywordsList == null || keywordsList.isEmpty()){
			System.out.println("Error:\tempty keywords list.");
			return 0;
		}
		else{
			Integer count = 0;			
			for(int i=0; i<keywordsList.size(); i++){
				Integer ccount= dict.get(keywordsList.get(i));
				if(ccount!=null) count += ccount;
			}
			
			return count;
		}
	}
	
	public Integer getWordsCount(){
		if(dict.isEmpty()){
			return 0;
		}
		else{
			Iterator iterator = dict.entrySet().iterator();
			Integer count = 0;
			while(iterator.hasNext()){
				iterator.next();
				count++;
			}
			
			return count;
		}
	}
	
	
	public void substractStopwordsList(ArrayList<String> stopwordsList){
		if(stopwordsList == null) return;
		
		for(int i=0; i<stopwordsList.size(); i++){
			String word = stopwordsList.get(i);			
			if(dict.containsKey(word)){
				dict.remove(word);
			}
		}
	}
	
	public void substractKnownKeywordsList(ArrayList<String> knownkeywordsList){
		if(knownkeywordsList == null) return;
		
		for(int i=0; i<knownkeywordsList.size(); i++){
			String word = knownkeywordsList.get(i);			
			if(dict.containsKey(word)){
				dict.remove(word);
			}
		}
	}
	
	public ArrayList<String> toKeywordsList(){
		ArrayList<String> keywords = new ArrayList<String>();
		
		Iterator iterator = dict.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry entry = (Map.Entry) iterator.next();
			keywords.add((String) entry.getKey());
		}
		
		return keywords;
	}
	
	public void print(){
		Iterator iterator = dict.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String, Integer> entry = (Entry<String, Integer>) iterator.next();
			System.out.println(entry.getKey()+": "+entry.getValue());
		}
	}
	
	public String toString(){
		return dict.toString();
	}
	
	public static void main(String[] args){
		Dictionary dict = new Dictionary();
		String text = "This is a very small text, for testing purposes only.";
		Pattern pattern = Pattern.compile("[</>*#-,.!?:\"\f\t\\v\\s]+");
		
		dict.parseString(text, pattern);
		dict.print();
		System.out.println(dict.toString());
		
	}

}
